package com.jiazhong.boot.mybatis.plus.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiazhong.boot.commons.bean.plus.Emp;
import com.jiazhong.boot.commons.util.JsonResult;
import com.jiazhong.boot.mybatis.plus.service.EmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@SpringBootTest
public class App {
    @Resource
    private EmpService service;

    @Test
    public void a() {
        List<Emp> list = service.list();
        log.info("list:{}", list);
    }

    @Test
    public void b() {
        Emp emp = new Emp();
        emp.setEname("刘嘉池");
        emp.setComm(98.9);
        emp.setSal(999.9);
        emp.setJob("网页设计");
        emp.setDeptNo(20);
        service.save(emp);
    }

    @Test
    public void c() {
        Emp emp = new Emp();
        emp.setEname("韦新悦");
        emp.setComm(198.9);
        emp.setSal(9929.9);
        emp.setJob("网页设计");
        emp.setDeptNo(30);
        emp.setEmpNo(8043);
        service.updateById(emp);
        //¬ service.saveOrUpdate(emp);
    }

    @Test
    public void d() {
        boolean b = service.removeById(8044);
        log.info("b:{}", b);
    }

    @Test
    public void e() {
        List<Integer> list = Arrays.asList(6666, 6664, 5454, 22312);
        boolean b = service.removeBatchByIds(list);
        log.info("b:{}", b);
    }


    @Test
    public void f1() {
        Emp emp = service.getById(7965);
        log.info("emp:{}", emp);
    }

    @Test
    public void f2() {
        List<Map<String, Object>> maps = service.listMaps();
        log.info("maps:{}", maps);
    }

    @Test
    public void f3() {
        List<Emp> list = service.listByIds(Arrays.asList(7936, 8040, 7941));
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void f4() {
        List<Object> list = service.listObjs();
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }
/*

    @Test
    public void f5() {
        PageHelper.startPage(1, 10);
        List<Emp> list = service.list();
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        log.info("pageInfo:{}", pageInfo);
    }
*/

    @Test
    public void f6() {
        IPage page = new Page(1, 10);
        List<Emp> list = service.list(page);
        // list不存在 设置集合
        page.setRecords(list);
        log.info("list:{}", list);
        log.info("page:{}", page.getPages());
        log.info("page:{}", page.getCurrent());
        log.info("page:{}", page.getSize());
        log.info("page:{}", page.getTotal());
        log.info("page:{}", page.getRecords());
    }

    // 条件构造器
    // 关系运算符
    @Test
    public void g1() {
        // 1. 产生了条件构造器对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 2. 设置条件 = > < >= <= !=
        // wrapper.eq("job", "程序员");
        // wrapper.ne("job","程序员");
        // wrapper.gt("sal","8000");
        // wrapper.ge("sal","8000");
        // wrapper.lt("sal",3000);
        wrapper.le("sal", 3000);
        // 3. 使用条件构造器进行查询操作
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    // 模糊查询
    @Test
    public void g2() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // wrapper.like("ename","张");
        // wrapper.notLike("ename","张");
        // wrapper.likeLeft("ename","张");
        wrapper.likeRight("ename", "张");
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void g3() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // wrapper.between("sal", 2000, 3000);
        // wrapper.notBetween("sal", 2000, 3000);
        // wrapper.in("job", Arrays.asList("a", "b", "c", "d"));
        wrapper.notIn("job", Arrays.asList("a", "b", "c", "d"));
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void g4() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // wrapper.orderByAsc("sal");
        // wrapper.orderByDesc("sal");
        wrapper.orderByDesc("sal", "job");
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void g5() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.groupBy("job");
        // 查询哪些列
        wrapper.select("job", "count(0)", "max(job)", "min(job)");
        wrapper.having("count(0)>0");
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void g6() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        /* 默认情况下，按照and拼接条件
        wrapper.eq("job", "程序员");
        wrapper.gt("sal", "8000");
        */
        wrapper
                .eq("job", "程序员")
                .or()
                .gt("sal", "18000");
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void g7() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.last("limit 1");
        // wrapper.first("a");
        List<Emp> list = service.list(wrapper);
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void h() {
        UpdateWrapper<Emp> wrapper = new UpdateWrapper<>();
        // 对那些列进行修改
        wrapper.set("sal", 3000);
        wrapper.set("deptNo", 20);
        // 对那些行进行修改 条件
        wrapper.eq("empNo", 8056);
        boolean b = service.update(wrapper);
        log.info("b:{}", b);
    }

    @Test
    public void i() {
        List<Emp> list = service.list();
        list.forEach(e -> {
            log.info("e:{}", e);
        });
    }

    @Test
    public void j() {
        JsonResult result = service.find();
        log.info("result:{}", result);
    }
}
